<?php
if(isset($_POST['action']))
{
    $db = new Database();
    $page = $_POST['page'];
    $cur_page = $page;
    $page -= 1;
    $per_page = 3; // Per page
    $previous_btn = true;
    $next_btn = true;
    $first_btn = true;
    $last_btn = true;
    $start = $page * $per_page;
    $action = $_POST['action'];
    $finaldata = "<table class='table_data'>
                     <thead>
                         <tr>
                              <th><input type='checkbox' name='selectAll' class='selectAll'/></th>
                              <th>Firstname</th>
                              <th>Lastname</th>
                              <th>User Type</th>
                              <!--th>Email</th-->
                              <th>Username</th>
			      <th>Division/Section</th>
			      <th>Location</th>
                              <th>Active</th>
                              <th>Actions</th>
                         </tr>
                     </thead>
                     <tbody>";
    if($action=="userSearch")
    {
        $value = $_POST['Name'];
	$documents = $db->query("SELECT * FROM users WHERE Firstname LIKE '%$value%' AND is_active='1' LIMIT $start, $per_page","array");
	$count = $db->query("SELECT * FROM users WHERE Firstname LIKE '%$value%' AND is_active='1'","numrows");
        $no_of_paginations = ceil($count / $per_page);
    }elseif($action=="default"){
	$documents = $db->query("SELECT * FROM users WHERE is_active='1' LIMIT $start, $per_page","array");
	$count = $db->query("SELECT * FROM users WHERE is_active='1'","numrows");
        $no_of_paginations = ceil($count / $per_page);
    }
    if($count>0){
      foreach($documents as $row_administrator)
	{
	    $db_userID = $row_administrator['userID'];
	    $db_userFirstname = $row_administrator['Firstname'];
	    $db_userLastname = $row_administrator['Lastname'];
	    $db_userType = $row_administrator['UserType'];
	    $db_UserDept = $row_administrator['UserDept'];
	    $db_UserLocation = $row_administrator['UserLocation'];
	    $db_userEmail = $row_administrator['UserEmailAdd'];
	    $db_userUsername = $row_administrator['Username'];
	    $db_userIsActive = $row_administrator['is_active'];
	$finaldata.= '
	<tr>
	    <td>
		<center><input type="checkbox" name="singleSelectAll[]" class="singleSelectAll" rel="'.$db_userID.'"/></center>
	    </td>
	    <td id="uFname_'.$db_userID.'">'.$db_userFirstname.'</td>
	    <td id="uLname_'.$db_userID.'">'.$db_userLastname.'</td>
	    <td id="uType_'.$db_userID.'" rel="'.$db_userType.'">';
	    $query_userType = $db->query("SELECT * FROM tbluser_level WHERE id = '$db_userType'","array");
	    foreach($query_userType as $row_usertype)
	    {
		$UserTypeID = $row_usertype['id'];
		$UserType = $row_usertype['level'];
		
	    $finaldata.= $UserType;
	    }
	
	$finaldata.='
	    <td id="uUname_'.$db_userID.'">'.$db_userUsername.'</td>
	    <td id="uUserDept_'.$db_userID.'">'.$db_UserDept.'</td>
	    <td id="uUserLocation_'.$db_userID.'">'.$db_UserLocation.'</td>';
	
	    if($db_userIsActive=="1"){
	$finaldata.='    
	    <td id="uActive_'.$db_userID.'" rel="'.$db_userIsActive.'"><center>Yes</center></td>';
	    }else{
	$finaldata.='    
	    <td id="uActive_'.$db_userID.'" rel="0"><center>No</center></td>';	
	    }
	$finaldata.='  
	    <td>
		<div id="actions_'.$db_userID.'">
		<center>
		    <input type="button" name="edit" id="'.$db_userID.'" class="edit_users" title="Edit User" style="border:0px solid;background:url(/images/icon/edit.png) no-repeat; width:24px; height:24px; cursor:pointer;"/>
		</center>
		</div>
	    </td>';
	$finaldata.='
	</tr>';
	}
        //pagination
        pagination($cur_page,$page,$per_page,$start,$previous_btn,$next_btn,$first_btn,$last_btn,$no_of_paginations,$finaldata,$count,$action,$value);
    }else{
        $finaldata.="<tr><td colspan='9'><center>No Record Found</center></td></tr>";
        echo $finaldata;
    }
}
?>
